#Region "Diclaimer"
'Copyright (c) 2006, Mark Bracewell (bracewell at wgg dot com)
'All rights reserved.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met: 
'*

'* Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in the
'documentation and/or other materials provided with the distribution.
'* The name Mark Bracewell may not be used to endorse or promote products
'derived from this software without specific prior written permission. 
'* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#End Region

Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.Net.Mail
Imports ClubStarterKit.Web
Imports ClubStarterKit.Data
Imports SubSonic

Namespace ClubStarterKit.Web

    Public Class ForumNotification
        Public Shared Sub Notify(ByVal forumMsg As ForumMessage)
            Try
                Dim MailFrom As MailAddress = New MailAddress(ClubStarterKit.Web.Settings.Webmaster(), ClubStarterKit.Web.Settings.Name())
                Dim mailMsg As New System.Net.Mail.MailMessage(MailFrom, MailFrom)
                mailMsg.IsBodyHtml = True
                mailMsg.Subject = ClubStarterKit.Web.Settings.Name() & " " & forumMsg.subject
                mailMsg.Body = FormatMsgBody(forumMsg)
                For Each usr As Member In Forum.WatcherList(forumMsg.threadid)
                    If usr.SendWatchEmails Then
                        mailMsg.Bcc.Add(New System.Net.Mail.MailAddress(usr.Email, usr.DisplayName))
                    End If
                Next
                Dim smtp As New System.Net.Mail.SmtpClient(ClubStarterKit.Web.Settings.MailServer())
                Dim cred As New System.Net.NetworkCredential(ClubStarterKit.Web.Settings.MailUsername(), ClubStarterKit.Web.Settings.MailPassword())
                smtp.UseDefaultCredentials = False
                smtp.Credentials = cred
                smtp.Send(mailMsg)
            Catch ex As Exception
                Throw
            End Try
        End Sub

        Public Shared Function FormatMsgBody(ByVal forumMsg As ForumMessage) As String
            Dim sb As StringBuilder = New StringBuilder
            sb.AppendLine(String.Format("<p><i>New message posted at <a href='" & ClubStarterKit.Web.Settings.SiteURL() & "forum/topics.aspx'>" & ClubStarterKit.Web.Settings.Name() & " Forums</a><br />by {0},<br />{1}</i></p>", forumMsg.displayname, forumMsg.postdate))
            sb.Append(String.Format("<p><b>{0}</b></p>", forumMsg.subject))
            sb.Append(forumMsg.body)
            sb.AppendLine(String.Format("<p><a href='" & ClubStarterKit.Web.Settings.SiteURL() & "forum/thread.aspx?th={0}'>Click here to visit this thread.</a></p>", forumMsg.threadid))
            sb.AppendLine("<p><i>Please do not reply to this email. Use the link above to visit the forum.</i></p>")
            Return sb.ToString()
        End Function

        Public Shared Sub ModerateEmail(ByVal modemail As String, ByVal RespondEmail As String, ByVal emailtext As String)
            Try
                Dim MailFrom As MailAddress = New MailAddress(ClubStarterKit.Web.Settings.Webmaster(), ClubStarterKit.Web.Settings.Name() & " Forum")
                Dim MailTo As MailAddress = New MailAddress(modemail)
                Dim replyto As MailAddress = New MailAddress(RespondEmail)
                Dim mailMsg As New System.Net.Mail.MailMessage(MailFrom, MailTo)
                mailMsg.IsBodyHtml = True
                mailMsg.Subject = ClubStarterKit.Web.Settings.Name() & " Forum Moderation Request"
                mailMsg.ReplyTo = replyto
                mailMsg.Body = emailtext
                Dim smtp As New System.Net.Mail.SmtpClient(ClubStarterKit.Web.Settings.MailServer())
                Dim cred As New System.Net.NetworkCredential(ClubStarterKit.Web.Settings.MailUsername(), ClubStarterKit.Web.Settings.MailPassword())
                smtp.UseDefaultCredentials = False
                smtp.Credentials = cred
                smtp.Send(mailMsg)
            Catch ex As Exception
            End Try
        End Sub

        Public Shared Sub ReportResolve(ByVal report As SpamFlag)
            Try
                Dim origionator As MembershipUser = Membership.GetUser(report.ReporterKey)
                Dim MailTo As MailAddress = New MailAddress(origionator.Email)
                Dim MailFrom As MailAddress = New MailAddress(ClubStarterKit.Web.Settings.Webmaster(), ClubStarterKit.Web.Settings.Name())
                Dim mailMsg As New System.Net.Mail.MailMessage(MailFrom, MailTo)
                mailMsg.IsBodyHtml = True
                Dim body As String = "<h2>Report Resolved</h2><br />"
                body += "We have reviewed your request and taken the appropriate action."
                mailMsg.Subject = "Report of Forum Message Resolved"
                mailMsg.Body = body
                Dim smtp As New System.Net.Mail.SmtpClient(ClubStarterKit.Web.Settings.MailServer())
                Dim cred As New System.Net.NetworkCredential(ClubStarterKit.Web.Settings.MailUsername(), ClubStarterKit.Web.Settings.MailPassword())
                smtp.UseDefaultCredentials = False
                smtp.Credentials = cred
                smtp.Send(mailMsg)
            Catch ex As Exception

            End Try
        End Sub

        Public Shared Sub ModeratePost(ByVal modemail As String, ByVal msg As ForumMessage)
            Try
                Dim MailFrom As MailAddress = New MailAddress(ClubStarterKit.Web.Settings.Webmaster(), ClubStarterKit.Web.Settings.Name() & " Automated Forum")
                Dim MailTo As MailAddress = New MailAddress(modemail)
                Dim mailMsg As New System.Net.Mail.MailMessage(MailFrom, MailTo)
                mailMsg.IsBodyHtml = True
                mailMsg.Subject = ClubStarterKit.Web.Settings.Name() & " Forum Moderation Request"

                Dim body As String = "<h2>Automated Forum Moderation</h2><br /><br />"
                body += "<strong>Post Title</strong><br />"
                body += msg.subject
                body += "<br /><br /><strong>Message Text</strong>:<br />"
                body += msg.body
                body += "<br /><br />"
                body += "<a href='" & ClubStarterKit.Web.Settings.SiteURL() & "forum/editpost.aspx?ms=" & msg.msgid & "'>Edit Message</a>"
                mailMsg.Body = body
                Dim smtp As New System.Net.Mail.SmtpClient(ClubStarterKit.Web.Settings.MailServer())
                Dim cred As New System.Net.NetworkCredential(ClubStarterKit.Web.Settings.MailUsername(), ClubStarterKit.Web.Settings.MailPassword())
                smtp.UseDefaultCredentials = False
                smtp.Credentials = cred
                smtp.Send(mailMsg)
            Catch ex As Exception
            End Try
        End Sub

    End Class

End Namespace